﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KongMing.ORM.SqlSugar.Models;
using KongMing.ORM.SqlSugar.QueryModels;
using SqlSugar;
using static KongMing.ORM.SqlSugar.SqlSugarHelper;

namespace KongMing.ORM.SqlSugar
{
    public class 连表查询
    {
        public static void JoinQuery()
        {
            using (var db = GetDB())
            {
                //1、基础连表
                var join_threeTable1 = db.Queryable<StoreBookStock, Book, BookStore>((sbs, b, bs) =>
                sbs.BookId == b.Id && sbs.BookStoreId == bs.Id)
                    .Select((sbs, b, bs) => new Join_BookStockModel
                    {
                        BookStoreName = bs.Name,
                        Address = bs.Address,
                        BookName = b.Title,
                        Author = b.Author,
                        InPrice = b.InPrice,
                        BookNum = sbs.BookNum
                    }).ToList();

                //通过joinType ：inner left right 连接方式连表
                var join_threeTable2 = db.Queryable<StoreBookStock, Book, BookStore>((sbs, b, bs) =>
                new JoinQueryInfos(
                    JoinType.Left, sbs.BookId == b.Id,
                    JoinType.Inner, sbs.BookStoreId == bs.Id
                )).Select((sbs, b, bs) => new Join_BookStockModel
                {
                    BookStoreName = bs.Name,
                    Address = bs.Address,
                    BookName = b.Title,
                    Author = b.Author,
                    InPrice = b.InPrice,
                    BookNum = sbs.BookNum
                }).ToList();
            }
        }
    }
}
